<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Create HTML list to show movies
 * @author Matthijs van Henten <matthijs@waag.org>
 *
 *
<video class="currentVideo" controls="true" autoplay="false" tabindex="0">
<source src="/movies/movie_5_DNA_dice_lecture_2.ogv"/>
</video>
*
 */
class Zend_View_Helper_MovieList{
	private $_movies;

	/**
	 * This function is used as a pseudo constructor by Zend_View
	 * and may be called as a function.
	 *
	 * @param Default_Model_Movie $movie to show
	 * 		The __set method is used to parse the variables
	 *
	 * 	@return string $pager HTML code for the pager
	 */
	public function MovieList( $movies ){
		$this->_movies = array();

		foreach( $movies as $movie ){
			if( $movie instanceof Default_Model_Movie ){
				$this->_movies[] = $movie;
			}
		}

		return $this->getList();
	}

	/**
	 * Render this pager by casting it to string
	 */
	public function __toString(){
		return $this->getList();
	}

	/**
	 * Explicit render of the player
	 *
	 * @return string $HTML Html code for this pager
	 */
	public function getList(){
		$html = array();

		//var_dump( $this->_movies );


		foreach( $this->_movies as $movie ){
			if( null == $movie->title ){
				continue;
			}
			$title = $this->getHTML('h4', $movie->title );

			$content = array();

			// picture
			$thumbnail = sprintf('<img class="thumbnail" src="/frame/thumbnail/%d/%d" />', $movie->id, $movie->posterFrame );
			$content[] = $this->getHTML( 'a', $thumbnail, array(
				'class' => 'thumbNailWrapper',
				'href'  => sprintf('/movie/play/%d', $movie->id )
			));

			$info = $movie->getInfo(); // ?? waarom als array?

			if( is_object( $info ) ){
				// metadata
				$date  = sprintf('<strong>%s </strong><span class="duration">%s</span>',
					$movie->date,
					array_shift( explode('.', $info->contentDuration) )
				);
			}

			$description = $this->getHTML('p', $movie->description );
			$content[] = $this->getHTML( 'div', $title . $date . $description, array('class' => 'movieMeta') );


			// buttons
			$play = $this->url( sprintf('/movie/play/%d', $movie->id ), 'play movie' );
			$annotate = $this->url( sprintf('/movie/annotate/%d', $movie->id ), 'annnotate movie' );

			$content[] = $this->getHTML( 'div', $play . $annotate, array('class' => 'buttonBar') );


			$html[] = $this->getHTML( 'li', join("\n", $content), array('class'	=> 'atMovieListItem') );

		}

		$html = join( "\n", $html );

		return $this->getHTML( 'ul', $html, array('class' => 'atMovieList') );

	}

	/**
	 * render a single HTML string using a variable tagname, content and attributes
	 *
	 * @param string $tagName Something of a HTML tagName such as 'div'
	 * @param string $content Content to put into the tags
	 * @param array $attributes A key_value pair of attributes
	 *
	 * @return string $HTML
	 */
	public function getHTML( $tagName, $content, $attributes=array() ){
		foreach( $attributes as $key => $value ){
			$attributes[$key] = sprintf('%s="%s"', $key, htmlspecialchars($value) );
		}

		return sprintf(
			"<%s%s>%s</%s>\n",
			$tagName,
			rtrim( ' ' . join(' ', $attributes) ),
			trim($content),
			$tagName
		);
	}

	public function url( $href, $content ){
		return sprintf("<a href=\"%s\">%s</a>\n", $href, $content );
	}

	/**
	 * return uri. a url may be given to prefix the pager with. url
	 * will be parsed so values in query string are preserved
	 *
	 * @return string $uri Uri passed to constructor or empty string
	 */
	public function getUri(){
		if( null == $this->uri ){
			$this->uri = '';
		}

		return $this->uri;
	}
}
